<?php
namespace app\addons\controller;
use think\Controller;
use think\Db;
use \think\Request;
// use app\index\
class Base extends Controller
{
    public $userMsg;
    public function _initialize()
    {
    	$this->checkLogin();
        $nowtime=date("Y-m-d H:i:s",time());
        $this->get_admin_channel();
        $this->get_admin_addons();
        $systems=db("systemt")->where("id=1")->find();
        $this->assign("systems",$systems);
        $this->assign("nowtime",$nowtime);
    }
    public function checkLogin(){
        $userMsg=session('userMsg');
        // print_r($userMsg);exit;
        if(!$userMsg['username']){
            $this->redirect('Login/index');
        }else{
            $this->userMsg=$userMsg;
            $this->assign("userMsg",$userMsg);
        }
    }
    public function get_admin_addons(){
        // $where['type']=1;
        $where['status']=1;
        $plugin=db("plugin")->where($where)->order("sort asc ,id asc")->select();
        
        // print_r($channel);exit;
        $this->assign("plugin",$plugin);
    }
    public function get_admin_channel(){
        // $where['type']=1;
        $where['status']=1;
        $channel=db("channel")->where($where)->order("sort asc ,id asc")->select();
        $modelx=db("modelx");
        for ($i=0; $i < count($channel); $i++) { 
            $channel[$i]['modelxName']=$modelx->where("id=".$channel[$i]['modelx'])->value("name");
            $channel[$i]['contro']=$modelx->where("id=".$channel[$i]['modelx'])->value("mark");
            $channel[$i]['level']=$modelx->where("id=".$channel[$i]['modelx'])->value("level");
            $channel[$i]['type']=$modelx->where("id=".$channel[$i]['modelx'])->value("type");
        }
        $channel=$this->makeTree($channel);
        // print_r($channel);exit;
        $this->assign("channel",$channel);
    }
    public function get_admin_menu(){
        $where['type']=1;
        $where['status']=1;
        $menu=db("menu")->where($where)->select();
        for ($i=0; $i < count($menu); $i++) { 
            if ($menu[$i]['pid'] !== 0) {
                $menu[$i]['catename']=db("menu")->where("id = ".$menu[$i]['pid'])->value("name");
            }else{
                $menu[$i]['catename']="无上级";
            }
        }
        // print_r($menu);exit;
        $this->assign("menu",$menu);
    }
    public function saveFile($file,$id=0)  {
        $ext=strrchr($file,".");
        $data['thumb']=$file;
        $data['type']=$ext; 
        $data['createtime']=time();
        if($id){
            $update['status']=0;
            db("filex")->where("id=".$id)->update($update);
        }
        $res=db("filex")->insert($data);                
        if(!$res){
            $mess['code']=100;
            $mess['msg']='附件保存失败';
            return json_encode($mess);
        }
        $returnId=db("filex")->order("id desc")->value("id");
        return $returnId;
    }
    public function default_upload(){
        // $file=$_FILES['file'];
        $file = request()->file('file');
        $fileInfo = $file->getInfo();
        // $post=$_POST;
        // return json_encode($file);
        // print_r($file);exit();
        $mess=$this->upload($file);
        if($mess['filename']){
            $data['filename']=$fileInfo['name'];
            $data['filesize']=$fileInfo['size'];
            $data['filetype']=$fileInfo['type'];
            $data['suffix']=$mess['suffix'];
            $data['urlpath']=$mess['filename'];
            // $data['createtime']=time();
            // $data['status']=1;
            $mess['data']=$data;
            // $res=db("filex")->insert($data);
            // $mess['data']=db("filex")->where($data)->order("id desc")->find();
        }
        return json_encode($mess);
    }
    public function upload_video(){
        // $file=$_FILES['file'];
        $file = request()->file('videox');
        // $post=$_POST;
        // return json_encode($file);
        // print_r($file);exit();
        $mess=$this->upload($file);
        if($mess['filename']){            
            $returns['errno']=0;
            $returns['data']['url']=$mess['filename'];
                       
        }else{
            $returns['errno']=1;
        } 
        return json_encode($returns);
    }
    public function upload_file(){
        // $file=$_FILES['file'];
        $file = request()->file('file');

        $mess=$this->upload($file);
        if($mess['filename']){
            $returns['errno']=0;
            $redata['url']=$mess['filename'];
            $redata['alt']=$mess['filename'];
            $redata['href']=$mess['filename'];
            $returns['data'][]=$redata;                 
        }else{
            $returns['errno']=1;
        }   
        return json_encode($returns);
    }
    public function upload_file_more(){
        $uploads="/uploads/";
        // $file=$_FILES['file'];
        $files = request()->file('image');
        // return json_encode($files);
        foreach($files as $file){
            // 移动到框架应用根目录/uploads/ 目录下
            $info = $file->move( ROOT_PATH . 'public' . DS . 'uploads');
            if($info){
                // 成功上传后 获取上传信息
                // 输出 jpg
                // echo $info->getExtension(); 
                // 输出 42a79759f284b767dfcb2a0197904287.jpg
                // echo $info->getFilename(); 
                $filename=$info->getSaveName();
                $mess['code']=1;
                $mess['msg']="附件已上传至服务器";
                $data['filename']=$uploads.$filename;
                $data['getFilename']=$info->getFilename(); 
                $data['getExtension']=$info->getExtension(); 
                $mess['data'][]=$data;
            }else{
                // 上传失败获取错误信息
                // echo $file->getError();
                $error=$file->getError();
                $mess['code']=0;
                $mess['msg']=$error;
                $mess['error']=$error;
            }    
        }
        return json_encode($mess);
    }
    public function upload($file){
        $uploads="/uploads/";
        // 获取表单上传文件 例如上传了001.jpg
        // $file = request()->file('image');
        
        // 移动到框架应用根目录/public/uploads/ 目录下
        if($file){
            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
            // $info = $file->rule('uniqid')->move(ROOT_PATH . 'public' . DS . 'uploads');
            if($info){
                // 成功上传后 获取上传信息
                // 输出 jpg
                // echo $info->getExtension();
                // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
                $filename=$info->getSaveName();
                $mess['code']=1;
                $mess['msg']="附件已上传至服务器";
                $mess['filename']=$uploads.$filename;
                $mess['suffix']=$info->getExtension();
                return $mess;
                // 输出 42a79759f284b767dfcb2a0197904287.jpg
                // return $info->getFilename(); 
            }else{
                // 上传失败获取错误信息
                $error=$file->getError();
                $mess['code']=0;
                $mess['msg']="上传失败";
                $mess['error']=$error;
                return $mess;
            }
        }
    }
    // 按照顺序构造一级数组，根据level判断层级
    public function getTree($array, $pid =0, $level = 0){

        //声明静态数组,避免递归调用时,多次声明导致数组覆盖
        static $list = [];
        foreach ($array as $key => $value){
            //第一次遍历,找到父节点为根节点的节点 也就是pid=0的节点
            if ($value['pid'] == $pid){
                //父节点为根节点的节点,级别为0，也就是第一级
                $value['level'] = $level;
                //把数组放到list中
                $list[] = $value;
                //把这个节点从数组中移除,减少后续递归消耗
                unset($array[$key]);
                //开始递归,查找父ID为该节点ID的节点,级别则为原级别+1
                $this->getTree($array, $value['id'], $level+1);

            }
        }
        return $list;
    }
    function makeTree($list, $pk='id', $pid = 'pid', $child = 'child', $root = 0) {
      // 创建Tree
      $tree = array();
      if(is_array($list)) {
        // 创建基于主键的数组引用
        $refer = array();
        foreach ($list as $key => $data) {
          $refer[$data[$pk]] =& $list[$key];
        }
        foreach ($list as $key => $data) {
          // 判断是否存在parent
          $parentId = $data[$pid];
          if ($root == $parentId) {
            $tree[] =& $list[$key];
          }else{
            if (isset($refer[$parentId])) {
              $parent =& $refer[$parentId];
              $parent[$child][] =& $list[$key];
            }
          }
        }
      }
      return $tree;
    }
}
